<?php
namespace Home\Controller;
use Think\Controller;
class CateController extends CommonController {
	
	//默认时间排序 也就是加载第一个
	public function index($where = '',$count = null,$order_by = 'create_time desc'){

		$cid = $_GET['cid'];

		//查询访问的分类id是否存在于数据库 存在的话就显示 不存在的话就提示非法访问
		$sql = "select id from category ";
		$result = M()->query($sql);
		foreach ($result as $key => $value) {
			$res[] = $value['id'];
		}
		// 查询是否存在
		if(!in_array($cid, $res)){
			$this->error('非法访问',U('home/index/index'));
		}

		// //查询总数的sql语句
		if($where == ''){
			$countsql = "select count(t1.id) count from question t1 left join category_question t2 on t1.id = t2.qid left join user t3 on t1.uid = t3.id  where t2.cid = {$cid}";
		}else{
			$countsql = "select count(t1.id) count from question t1 left join category_question t2 on t1.id = t2.qid left join user t3 on t1.uid = t3.id  where t2.cid = {$cid} and {$where}";
		}
		
		$count = is_null($count) ? current(M()->query($countsql))['count'] : $count;

		$Page  = new \Think\Page($count,1);// 实例化分页类 传入总记录数和每页显示的记录数(1)
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$Page->setConfig('first','首页');
		//分页跳转的时候保证查询条件
		foreach($map as $key=>$val) {
		    $Page->parameter[$key]   =   urlencode($val);
		}

		$show  = $Page->show();// 分页显示输出

		//查询get到的cid 查询出对应的类别的id作为条件来输出
		//如果条件参数为空 则默认显示时间的
		if($where == ''){
			$sql = "select *,t1.id as qid,t3.id as userid ,t2.id as cqid from question t1 left join category_question t2 on t1.id = t2.qid left join user t3 on t1.uid = t3.id  where t2.cid = {$cid} order by $order_by limit {$Page->firstRow},{$Page->listRows}";
		}else{
			$sql = "select *,t1.id as qid,t3.id as userid ,t2.id as cqid from question t1 left join category_question t2 on t1.id = t2.qid left join user t3 on t1.uid = t3.id  where t2.cid = {$cid} and {$where} order by $order_by limit {$Page->firstRow},{$Page->listRows}";
		}

		$res = M()->query($sql);

		foreach ($res as $k =>$value) {
			// 判断是否有回答 有人回答的话显示回答的人的名字
			if($value['answer_num'] > 0){
				$answerSql = "select t1.username,t2.create_time,t1.id as userid from user t1 left join answer t2 on t1.id = t2.uid where t2.qid = {$value['qid']} order by create_time desc limit 1" ;
				$answerResult = M()->query($answerSql);
				$res[$k]['username'] = $answerResult[0]['username']; 		 //替换回答者的名字
				$res[$k]['create_time'] = $answerResult[0]['create_time'];   //替换回答者回答的时间
				$res[$k]['userid'] = $answerResult[0]['userid'];
			}
			//获得标签名字
			$cateSql = "select t1.cname,t1.id as cid from category t1 left join category_question t2 on t1.id = t2.cid where t2.qid = {$value['qid']}";
			$cateResult = M()->query($cateSql);	
			$res[$k]['category'] = $cateResult;
			$res[$k]['create_time'] = Ftime(date('Y-m-d H:i:s',$value['create_time'])); //转换时间
			
		}

		//查询点击过来的分类的信息 例如javascript  cut_str
		$cateDataSql = "select * from category where id = {$cid}";
		$cateDataRes = M()->query($cateDataSql)[0];
		//对对应分类的介绍的字符进行处理
		$cateDataRes['cintroduce'] = GetPartStr($cateDataRes['cintroduce'],95);
		
		$this->assign('cateData',$cateDataRes);
		$this->assign('questionRes',$res);
		$this->assign('page',$show);     //渲染分页
		$this->display('cate/index');
	}

	//根据投票的排序
	public function voteindex(){
		$this->index('',null,'vote_num desc');
	}
	//未回答的
	public function unswerindex(){
		$this->index('answer_num = 0');
	}
	
	//未解决的
	public function nodealindex(){
		$this->index(' t1.status != 4');
	}

	public function explain(){
		$cid = $_GET['cid'];
		//查询点击过来的分类的信息 例如javascript  
		$cateDataSql = "select * from category where id = {$cid}";
		$cateDataRes = M()->query($cateDataSql)[0];
		$cateDetilInfor = $cateDataRes['cintroduce'];
		//对对应分类的介绍的字符进行处理
		$cateDataRes['cintroduce'] = GetPartStr($cateDataRes['cintroduce'],95);
		$this->assign('cateData',$cateDataRes);
		$this->assign('cateDetilInfor',$cateDetilInfor);
		$this->display();
	}

}